/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/3/14
 */
import React, {Component} from 'react';
import {connect} from "react-redux"
import {Button, Form, Input, message, Radio, Select} from "antd"
import {findPersonalSettingInfo, userInfoSave} from "../service";
import {hide, show} from "../../../configs/SharedReducer";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const formItemLayout = {
  labelCol: {span: 8},
  wrapperCol: {span: 12}
}

class BaseInfo extends Component {
  state = {
    initValue: null
  }

  componentDidMount() {
    this.getDataSource();
  }

  getDataSource = () => {
    const {userId} = this.props;
    this.props.show();
    findPersonalSettingInfo({userId})
      .then(res => {
        this.setState({initValue: res});
      })
      .finally(() => {
        this.props.hide();
      })
  }
  handleSave = () => {
    const {initValue} = this.state;
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        let params = {...initValue,...values}
        this.props.show();
        userInfoSave(params).then(result => {
          if (result.status === "SUCCESS") {
            message.success(result.message ? result.message : "请求成功");
            //刷新本地数据
            this.getDataSource();
          } else {
            message.error(result.message ? result.message : "请求失败");
          }
        }).catch(e => {
        }).finally(() => {
          this.props.hide();
        })
      }
    })
  }
  render() {
    const {initValue} = this.state;
    const {getFieldDecorator} = this.props.form;
    return (
      <Form style={{width: 600}}>
        <FormItem
          label={"姓名"}
          {...formItemLayout}>
          {getFieldDecorator('userName', {
            initialValue: initValue && initValue.user ? initValue.user.userName : "",
          })(
            <Input disabled/>
          )}
        </FormItem>
        <FormItem
          label={"用户类型"}
          {...formItemLayout}>
          {getFieldDecorator('userTypeRemark', {
            initialValue: initValue ? initValue.userTypeRemark : "",
          })(
            <Input disabled/>
          )}
        </FormItem>
        <FormItem
          label={"组织机构"}
          {...formItemLayout}>
          {getFieldDecorator('organizationName', {
            initialValue: initValue ? initValue.organizationName : "",
          })(
            <Input disabled/>
          )}
        </FormItem>
        <FormItem
          label={"员工编号"}
          {...formItemLayout}>
          {getFieldDecorator('code', {
            initialValue: initValue ? initValue.code : "",
          })(
            <Input disabled/>
          )}
        </FormItem>

        <FormItem
          {...formItemLayout}
          label="性别">
          {getFieldDecorator('gender', {
            initialValue: initValue ? initValue.gender : "",
          })(
            <Radio.Group>
              <Radio value={true}>男</Radio>
              <Radio value={false}>女</Radio>
            </Radio.Group>
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="身份证号">
          {getFieldDecorator('idCard', {
            initialValue: initValue ? initValue.idCard : "",
          })(
            <Input/>
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="邮箱">
          {getFieldDecorator('email', {
            initialValue: initValue ? initValue.email : "",
          })(
            <Input/>
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="移动电话">
          {getFieldDecorator('mobile', {
            initialValue: initValue ? initValue.mobile : "",
          })(
            <Input/>
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="语言代码">
          {getFieldDecorator('languageCode', {
            initialValue: initValue&&initValue.languageCode ? initValue.languageCode : "zh_CN",
          })(
            <Select>
              <Select.Option value={"zh_CN"}>中文(简体)</Select.Option>
            </Select>
          )}
        </FormItem>
        <FormItem
          {...formItemLayout}
          label="记账用户"
        >
          {getFieldDecorator('accountor', {
            initialValue: initValue ? initValue.accountor : "",
          })(
            <Input/>
          )}
        </FormItem>
        <br/>
        <div style={{width: "100%",display: "flex",justifyContent: "center"}}>
          <Button type={"primary"} onClick={this.handleSave}>保存</Button>
        </div>
      </Form>
    );
  }
}
export default connect(null,{show,hide})(Form.create()(BaseInfo));
